package member.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import member.service.MemberServiceImpl;
import member.vo.MemberVO;
import member.vo.ZipCodeVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MemberController {
	public MemberServiceImpl service;
	
	public MemberController(MemberServiceImpl service){
		this.service = service;
	}
	
	
	/**
	 * 로그인 메소드
	 * 개발자: bum
	 * 날짜: 2013. 3. 2. 시간: 오후 10:31:26
	 * @param session
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/login.do"})
	public ModelAndView login(HttpSession session,MemberVO vo){
		ModelAndView view = new ModelAndView();
		try {
			MemberVO mvo = service.login(vo);
			if(mvo!=null){
				if(vo.getMemScrtNo().equals(mvo.getMemScrtNo())){
					session.setAttribute("login_info", mvo);
					view.setViewName("member/login_success");
				}else{
					view.addObject("error_message", "패스워드가 틀렸습니다.");
					view.setViewName("loginFail/loginFail");
				}
			}else{
				view.addObject("error_message", "로그인에 실패하였습니다.");
				view.setViewName("loginFail/loginFail");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message",e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 주소 검색
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오전 1:04:40
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value={"/util/zipcode.do"})
	public ModelAndView zipcode(HttpServletRequest request, HttpServletResponse response){
		ModelAndView view = new ModelAndView();
		String dong = request.getParameter("dong");
		try {
			view.addObject("list", service.zipCodeSelect(dong));
			view.setViewName("jsonView");
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 아이디 중복 여부 체크
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오전 1:40:34
	 * @param request
	 * @return
	 */
	@RequestMapping(value={"/member/chek_id.do"})
	public ModelAndView checkId(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			view.addObject("message", service.checkId(request.getParameter("id")));
			view.setViewName("jsonView");
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 회원가입
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오전 11:11:02
	 * @param session
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/member/join.do"})
	public ModelAndView memberJoin(HttpSession session, MemberVO vo){
		ModelAndView view = new ModelAndView();
		try {
			vo = service.memberJoin(vo);
			if(vo!=null){
				session.setAttribute("login_info", vo);
				view.setViewName("member/login_success");
			}else{
				view.setViewName("loginFail/error");
			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		} catch (IOException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 로그아웃
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오전 11:40:27
	 * @param session
	 * @return
	 */
	@RequestMapping(value={"/loginOut.do","/member/loginOut.do"})
	public ModelAndView memberLoginOut(HttpSession session){
		ModelAndView view = new ModelAndView();
		if(session != null){
			session.invalidate();
			view.setViewName("Login");
		}
		return view;
	}
	
	/**
	 * 아이디 찾기
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 12:27:51
	 * @param request
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/member/sarchId.do"})
	public ModelAndView serchId(HttpServletRequest request,MemberVO vo){
		ModelAndView view = new ModelAndView();
		try {
			vo = service.serchId(vo);
			view.addObject("vo", vo);
			view.setViewName("jsonView");
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 이메일 발송 
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 1:18:59
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/member/sendMail.do"})
	public ModelAndView sendMail(MemberVO vo){
		ModelAndView view = new ModelAndView();
		try {
			service.sendMail(vo);
			view.addObject("message", "메일을 성공적으로 보냈습니다.");
			view.setViewName("success");
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		} catch (MessagingException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 패스워드 찾기
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 1:39:22
	 * @param request
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/member/serach_pass.do"})
	public ModelAndView searchPass(HttpServletRequest request,MemberVO vo){
		ModelAndView view = new ModelAndView();
		try {
			vo = service.searchPass(vo);
			view.addObject("mem", vo);
			view.setViewName("serachPass");
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 패스워드 수정
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 1:32:36
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/member/changePass.do"})
	public ModelAndView changePassword(MemberVO vo){
		ModelAndView view = new ModelAndView();
		try {
			int count = service.changePassword(vo);
			if(count == 1){
				view.addObject("message", "패스워드 수정에 성공하였습니다.");
				view.setViewName("success");
			}else{
				view.addObject("error_message", "패스워드 수정에 실패하였습니다.");
				view.setViewName("loginFail/loginFail");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			view.addObject("error_message", e.getMessage());
			view.setViewName("loginFail/error");
		}
		return view;
	}
	
	/**
	 * 회원 정보 수정 패이지로 이동
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 2:41:11
	 * @param session
	 * @return
	 */
	@RequestMapping(value={"/modifyView.do","/member/modifyView.do"})
	public ModelAndView member_modify(HttpSession session){
		ModelAndView view = new ModelAndView();
		MemberVO mvo = (MemberVO)session.getAttribute("login_info");
		if(mvo != null){
			view.setViewName("member/member_modify");
		}else{
			view.addObject("error_message", "로그인을 먼저해주세요");
			view.setViewName("loginFail/loginFail");
		}
		return view;
	}
	
	/**
	 * 회원 정보 수정
	 * 개발자: bum
	 * 날짜: 2013. 3. 3. 시간: 오후 3:07:25
	 * @param session
	 * @param vo
	 * @return
	 */
	@RequestMapping(value={"/member_modify.do","/member/member_modify.do"})
	public ModelAndView memberModify(HttpSession session, MemberVO vo){
		ModelAndView view = new ModelAndView();
		MemberVO mvo = (MemberVO)session.getAttribute("login_info");
		if(session.getAttribute("flag")==null){
			return new ModelAndView("member/member_modify_success","login_info",mvo);
		}
		session.removeAttribute("flag");
		if(mvo == null){
			view.setViewName("loginFail/loginFail");
		}else{
			try {
				service.memberModify(vo);
				mvo = vo;
				if(vo.getUpfile() !=null && !vo.getUpfile().isEmpty()){
					mvo.setPicture_name(vo.getPicture_name());
				}
				session.setAttribute("login_info", mvo);
				view.setViewName("member/member_modify_success");
			} catch (IllegalStateException e) {
				e.printStackTrace();
				view.addObject("error_message", e.getMessage());
				view.setViewName("loginFail/error");
			} catch (SQLException e) {
				e.printStackTrace();
				view.addObject("error_message", e.getMessage());
				view.setViewName("loginFail/error");
			} catch (IOException e) {
				e.printStackTrace();
				view.addObject("error_message", e.getMessage());
				view.setViewName("loginFail/error");
			}
		}
		return view;
	}
}
















